home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Cheat II / cheatWindow.c < prev    next >
Text File  |  1995-09-05  |  8KB  |  338 lines

  1. /*****
  2.  * cheatWindow.c
  3.  *
  4.  *
  5.  *****/
  6.  
  7. #include <Processes.h>
  8. #include "cheatWindow.h"
  9. #include "main.h"
  10. #include "ResRefs.h"
  11. #include "cheat.h"
  12. #include "error.h"
  13. #include "cheatwich.h"
  14.  
  15. #define krefreshrate 60
  16.  
  17. Rect        listRect = {170, 150, 280, 365};
  18. Rect        slistRect = {84, 8, 240, 230};
  19. Rect        picrect = {4, 4, 36, 36};
  20.  
  21. WindowPtr    cheatWindow, sheetWindow;
  22. Handle        ditem[riNumItems], sitem[riSheetItems];
  23. Rect        dragRect;
  24. int useColor = false, cheatWindDepth = 1;
  25. ListHandle plist, slist;
  26.  
  27. ProcessInfoRec InfoRec[maxProcs];
  28. long numProcs = 0;
  29. ProcessSerialNumber usPSN;
  30.  
  31.  
  32.     // it's an old way to do it, and though Apple doesn't promise to support it,
  33.     //        I bet they will
  34. static void CheckForColor()
  35. {
  36.     SysEnvRec se;
  37.     if (SysEnvirons(1, &se) == 0)
  38.         if (se.hasColorQD)
  39.                 useColor = true;
  40. }
  41.  
  42.  
  43.     // part of following function borrowed from GPS by Glen Lalonde
  44. static void readProcTable(void)
  45. {
  46.      ProcessSerialNumber process;
  47.      OSErr res;
  48.      ProcessInfoRec recTemp;
  49.      Boolean same;
  50.      
  51.      process.highLongOfPSN = 0;
  52.      process.lowLongOfPSN = kNoProcess;
  53.      
  54.      verify(!GetCurrentProcess(&usPSN));
  55.      
  56.      numProcs = 0;
  57.      
  58.      while( GetNextProcess(&process) == noErr) {
  59.              // first make sure this process isn't us, we don't want to cheat on ourselves
  60.          res = SameProcess(&process, &usPSN, &same);
  61.          if (res || same)
  62.              continue;        // it is us
  63.          
  64.         InfoRec[numProcs].processInfoLength = sizeof(ProcessInfoRec);
  65.         if (!InfoRec[numProcs].processName) 
  66.             InfoRec[numProcs].processName = (void *)NewPtr(32);
  67.         InfoRec[numProcs].processAppSpec = 0;
  68.         
  69.         if ((res = GetProcessInformation(&process, &(InfoRec[numProcs]))) != noErr) 
  70.             verify(false);
  71.         numProcs++;
  72.         if (numProcs > maxProcs) break; /* too many to handle */
  73.      }
  74.      if (numProcs > maxProcs)
  75.          numProcs = maxProcs;
  76. }
  77.  
  78.  
  79. static void addProcsToList(void)
  80. {
  81.     short i;
  82.     Cell cell = {0, 0};
  83.     
  84.     (void) LAddRow(numProcs, 0, plist);
  85.     for (i=0;i<numProcs;i++) {
  86.         cell.v = i;
  87.         LSetCell((Ptr) &(InfoRec[i].processName[1]), InfoRec[i].processName[0], cell, plist);
  88.     }
  89. }
  90.  
  91.  
  92. void refreshProcesses(void)
  93. {
  94.     ProcessSerialNumber oldpsn[maxProcs], oldselection;
  95.     short i, oldNumProcs, changed, didselect;
  96.     static unsigned long lastcall = 0;
  97.     Cell cell = {0, 0};
  98.     Boolean same;
  99.     OSErr res;
  100.     
  101.     if (TickCount() - lastcall < krefreshrate)
  102.         return;        // hasn't been long enough
  103.     lastcall = TickCount();
  104.     
  105.         // first, we save the old numbers
  106.     oldNumProcs = numProcs;
  107.     for (i=0;i<numProcs;i++)
  108.         oldpsn[i] = InfoRec[i].processNumber;
  109.         
  110.         // remember selection in terms of a PSN
  111.     if (didselect = LGetSelect(true, &cell, plist))
  112.         oldselection = InfoRec[cell.v].processNumber;
  113.         
  114.         // find current process list
  115.     readProcTable();
  116.     
  117.         // find if there's a difference
  118.     changed = false;
  119.     if (numProcs != oldNumProcs)
  120.         changed = true;
  121.     else
  122.         for (i=0;i<numProcs;i++)    // check to see if numbers the same
  123.             changed |=
  124.                 (oldpsn[i].highLongOfPSN != InfoRec[i].processNumber.highLongOfPSN) ||
  125.                 (oldpsn[i].lowLongOfPSN != InfoRec[i].processNumber.lowLongOfPSN);
  126.     
  127.         // now, if things have changed we have to clear old list and make new one
  128.     if (changed) {
  129.         LDoDraw(false, plist);
  130.         LDelRow(0, 0, plist);        // remove contents of list
  131.         addProcsToList();            // add them again
  132.         
  133.             // reselect old process if there was one selected
  134.         if (didselect)
  135.             for (i=0;i<numProcs;i++) {
  136.                 res = SameProcess(&oldselection, &InfoRec[i].processNumber, &same);
  137.                 cell.v = i;
  138.                  if (same)
  139.                      LSetSelect(true, cell, plist);
  140.             }
  141.         
  142.         LDoDraw(true, plist);
  143.         SetPort(cheatWindow);
  144.         InvalRect(&listRect);        // so it draws on update
  145.         EraseRect(&listRect);
  146.     }
  147. }
  148.  
  149.  
  150. static void MakeAList(void)
  151. {
  152.     Rect dataBounds = {0, 0, 0, 1};
  153.     Point cSize = {0, 0};
  154.     
  155.     plist = LNew(&listRect, &dataBounds, cSize, 0, cheatWindow, true, false, false, true);
  156.     verify(plist);
  157.     readProcTable();
  158.     addProcsToList();
  159. }
  160.  
  161.  
  162. static void InitCheatSheet(void)
  163. {
  164.     Rect dataBounds = {0, 0, 0, 1};
  165.     Point cSize = {0, 0};
  166.     int i;
  167.     Handle h;
  168.     Cell cell;
  169.     
  170.     dataBounds.bottom = Count1Resources(kcheatsheettype);
  171.     slist = LNew(&slistRect, &dataBounds, cSize, 0, sheetWindow, true, false, false, true);
  172.     verify(slist);
  173.     cut = (shortcut **) NewHandle(dataBounds.bottom * sizeof(shortcut));
  174.     verify(cut);
  175.     HLock((Handle) cut);
  176.         // get all the shortcuts from the resources
  177.     cell.h = 0;
  178.     for (i=1;i<=dataBounds.bottom;i++) {
  179.         h = Get1IndResource(kcheatsheettype, i);
  180.         verify(h);
  181.         HLock((Handle) h);
  182.         (*cut)[i-1] = **((shortcut **) h);        // move the data over
  183.         cell.v = i - 1;
  184.             // add to list
  185.         LSetCell((Ptr) &((*cut)[i-1].title[1]), (*cut)[i-1].title[0], cell, slist);
  186.         HUnlock((Handle) h);
  187.         ReleaseResource(h);
  188.     }
  189.     HUnlock((Handle) cut);
  190.     gnumcuts = dataBounds.bottom;
  191. }
  192.  
  193.  
  194.     // futzes with radio boxes
  195. void setSearchSize(short newsize)
  196. {
  197.     short i;
  198.     
  199.     gvalsize = newsize;
  200.     for (i=0;i<3;i++)
  201.         SetCtlValue((ControlHandle) ditem[i+riSizeLong], newsize == i);
  202. }
  203.  
  204.     // futzes with radio boxes
  205. void setFindGameBy(short newval)
  206. {
  207.     short i;
  208.     
  209.     ghowfind = newval;
  210.     for (i=0;i<2;i++)
  211.         SetCtlValue((ControlHandle) ditem[i+riSelecting], ghowfind == i);
  212. }
  213.  
  214.  
  215. void SetUpWindow(void)
  216. {
  217.     PixMapHandle ppix;
  218.     short i, type;
  219.     Rect box;
  220.     short px, py;
  221.  
  222.     dragRect = screenBits.bounds;
  223.     CheckForColor();
  224. //    if (useColor)
  225. //        cheatWindow = (WindowPtr) NewCWindow(0L, &windowBounds, "\pUntitled", true, noGrowDocProc, (WindowPtr) -1L, true, 0);
  226. //    else
  227.  
  228.         // first we put up the cheat sheet, as it's behind
  229.     sheetWindow = GetNewDialog(rSheetDialog, 0, (WindowPtr) -1L);
  230.     verify(sheetWindow);
  231.  
  232.     py = 50;
  233.     px = 491;    // desired left position of window
  234.     if (px + 258 + 8 > screenBits.bounds.right)
  235.         px = screenBits.bounds.right - 258 - 8;
  236.     MoveWindow((WindowPtr) sheetWindow, px, py, false);
  237.     ShowWindow((WindowPtr) sheetWindow);
  238.  
  239.         // get items in dialog for future reference
  240.     for (i=1;i<riSheetItems;i++) {
  241.         GetDItem(sheetWindow, i, &type, &sitem[i], &box);
  242.         verify(sitem[i]);
  243.     }
  244.     HiliteControl((ControlHandle) sitem[riExportNew], 255);
  245.     HiliteControl((ControlHandle) sitem[riAppendToOld], 255);
  246.     HiliteControl((ControlHandle) sitem[riExportAppend], 255);
  247.     HiliteControl((ControlHandle) sitem[riOpenCheat], 255);
  248.     HiliteControl((ControlHandle) sitem[riRemoveCheat], 255);
  249.     HiliteControl((ControlHandle) sitem[riAddCheat], 255);
  250.     InitCheatSheet();
  251.     
  252.     cheatWindow = GetNewDialog(rOurDialog, 0, (WindowPtr) -1L);
  253.     verify(cheatWindow);
  254.     SetPort(cheatWindow);
  255.         
  256.         // get items in dialog for future reference
  257.     for (i=1;i<riNumItems;i++) {
  258.         GetDItem(cheatWindow, i, &type, &ditem[i], &box);
  259.         verify(ditem[i]);
  260.     }
  261.     setSearchSize(ksizeint);
  262.     setFindGameBy(kfindselect);
  263.     
  264.     if (useColor) {
  265.         ppix = ((CWindowPtr) cheatWindow)->portPixMap;
  266.         verify(ppix);
  267.         cheatWindDepth = (**ppix).pixelSize;
  268.         if (cheatWindDepth < 4)
  269.             useColor = false;
  270.         else {        // get pixpats etc.
  271.         }
  272.     }
  273.     
  274.     MakeAList();
  275. }
  276.  
  277. void DrawFoundPic(void)
  278. {
  279.     static CIconHandle sunc = 0, cloudc = 0;
  280.     
  281.     SetPort(cheatWindow);
  282.     if (!sunc) {        // read from resource file
  283.         sunc = GetCIcon(rSunCicn); verify(sunc);
  284.         cloudc = GetCIcon(rCloudsCicn); verify(cloudc);
  285.     }
  286.     if (useColor)
  287.         if (gcurposs == 1)
  288.             PlotCIcon(&picrect, sunc);
  289.         else
  290.             PlotCIcon(&picrect, cloudc);
  291. }
  292.  
  293.     // called when the interruptor has done stuff and we need to redraw screen etc
  294. void doclearup(void)
  295. {
  296.     Str255 str;
  297.  
  298.     addwichcuts();
  299.         // change the text field of the possibilities item
  300.     NumToString(gcurposs, str);
  301.     SetIText(ditem[riPossibilities], str);
  302.     
  303.         // change the text field of the offset item
  304.     if (gcurposs)
  305.         NumToString(goffset, str);
  306.     else
  307.         str[0] = 0;        // empty string
  308.     SetIText(ditem[riOffset], str);
  309.     
  310.     setSearchSize(gvalsize);    // make sure radio buttons correctly highlighted
  311.     DrawFoundPic();
  312.     
  313.     fixshortbuttz();
  314. }
  315.  
  316.  
  317. void DrawCheatWindow(void)
  318. {
  319.     Rect r = listRect;
  320.     
  321.     SetPort(cheatWindow);
  322.     InsetRect(&r, -1, -1);
  323.     FrameRect(&r);
  324.     LUpdate(cheatWindow->visRgn, plist);
  325.     
  326.     DrawFoundPic();
  327. }
  328.  
  329.  
  330. void DrawSheetWindow(void)
  331. {
  332.     Rect r = slistRect;
  333.     
  334.     SetPort(sheetWindow);
  335.     InsetRect(&r, -1, -1);
  336.     FrameRect(&r);
  337.     LUpdate(sheetWindow->visRgn, slist);
  338. }